package leetcode.hot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Solution49 {

    public static void main(String[] args) {
        String[] ss = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(new Solution49().groupAnagrams(ss));
    }

    public List<List<String>> groupAnagrams(String[] strs) {
//        ArrayList<ArrayList<String>> total = new ArrayList<>();
        //分到一起的字符串应该具有这样的特征：字符统计相同，a+time[a]+b+time[b]+...+z+time[z]
        HashMap<String,ArrayList<String>> featureAndList = new HashMap<>();
        for (String str : strs) {
            String feature = getFeature(str);
            System.out.println(feature);
            ArrayList<String> arrayList = featureAndList.getOrDefault(feature,new ArrayList<>());
            arrayList.add(str);
            featureAndList.put(feature,arrayList);
        }
        return new ArrayList<>(featureAndList.values());
    }

    public String getFeature(String s){
        char[] ca = new char[26];
        for (int i = 0; i < s.length(); i++) {
            ca[s.charAt(i)-'a']++;
        }
//        StringBuffer sb = new StringBuffer();
//        for (int i = 0; i < counts.length; i++) {
//            sb.append((char)('a'+i)+counts[i]);
//        }
//        return sb.toString();
        return String.valueOf(ca);
    }
}
